package com.lanou.moving.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lanou.moving.R;
import com.lanou.moving.adapter.RankingListAdapter;
import com.lanou.moving.base.BaseActivity;
import com.lanou.moving.base.BaseApplication;
import com.lanou.moving.constant.URL;
import com.lanou.moving.entity.RankingList;
import com.lanou.moving.volley.StringRequestUtf;
import com.lanou.moving.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

public class RankingListActivity extends BaseActivity implements
        AdapterView.OnItemClickListener, View.OnClickListener {


    private VolleySingleton volleySingleton;
    private List<RankingList> rankingLists;
    private List<RankingList.RefsEntity> refsEntityLists;
    private ArrayList<String> names;// 榜分类
    private ArrayList<String> sub_titles;// 更新时间
    // 官方榜
    private TextView office_title;
    private ListView office_content;
    private View office_view;
    // 特色榜
    private TextView special_title;
    private ListView special_content;
    private View special_view;
    // 全球榜
    private TextView world_title;
    private ListView world_content;
    private View world_view;

    private ImageView singer_back, search;
    private TextView singer;

    private RankingListAdapter adapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking_list);
        BaseApplication.getInstance().addActivity(this);
        initView();
        getData();
    }

    public void initView() {
        volleySingleton = VolleySingleton.getInstance(this);
        office_title = (TextView) findViewById(R.id.office_title);
        office_content = (ListView) findViewById(R.id.office_content);
        office_content.setOnItemClickListener(this);
        office_view = findViewById(R.id.office_view);

        special_title = (TextView) findViewById(R.id.special_title);
        special_content = (ListView) findViewById(R.id.special_content);
        special_content.setOnItemClickListener(this);
        special_view = findViewById(R.id.special_view);

        world_title = (TextView) findViewById(R.id.world_title);
        world_content = (ListView) findViewById(R.id.world_content);
        world_content.setOnItemClickListener(this);
        world_view = findViewById(R.id.world_view);

        singer_back = (ImageView) findViewById(R.id.singer_back);
        singer_back.setOnClickListener(this);
        search = (ImageView) findViewById(R.id.search);
        search.setOnClickListener(this);
        singer = (TextView) findViewById(R.id.singer);
        singer.setText("排行榜");
    }

    private void getData() {
        names = new ArrayList<>();
        sub_titles = new ArrayList<>();
        StringRequestUtf stringRequestUtf = new StringRequestUtf(URL.RANKING_LIST_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                rankingLists = gson.fromJson(response, new TypeToken<List<RankingList>>() {
                }.getType());

                office_view.setVisibility(View.VISIBLE);
                special_view.setVisibility(View.VISIBLE);
                world_view.setVisibility(View.VISIBLE);

                adapter = new RankingListAdapter(RankingListActivity.this, rankingLists, "官方榜");
                office_content.setAdapter(adapter);

                adapter = new RankingListAdapter(RankingListActivity.this, rankingLists, "特色榜");
                special_content.setAdapter(adapter);

                adapter = new RankingListAdapter(RankingListActivity.this, rankingLists, "全球榜");
                world_content.setAdapter(adapter);

                for (RankingList rankingList : rankingLists) {
                    names.add(rankingList.getName());
                    refsEntityLists = rankingList.getRefs();
                }

                for (RankingList.RefsEntity refsEntityList : refsEntityLists) {
                    sub_titles.add(refsEntityList.getSub_title());
                }

                office_title.setText(names.get(0) + "-" + sub_titles.get(0));
                special_title.setText(names.get(1) + "-" + sub_titles.get(1));
                world_title.setText(names.get(2) + "-" + sub_titles.get(2));


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        volleySingleton.addQueue(stringRequestUtf);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        RankingList.RefsEntity refsEntity = (RankingList.RefsEntity) parent.getItemAtPosition(position);
        if (refsEntity.getRanklist_id() != -1) {
            int ranklist_id = refsEntity.getRanklist_id();
            Intent intent = new Intent(RankingListActivity.this, RankingListDetailActivity.class);
            intent.putExtra("ranklist_id", ranklist_id);
            startActivity(intent);
        } else {
            String link = refsEntity.getLink();
            Intent intent = new Intent(RankingListActivity.this, WebViewActivity.class);
            intent.putExtra("link", link);
            startActivity(intent);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.singer_back:
                finish();
                break;
            case R.id.search:
                Intent intent = new Intent(RankingListActivity.this, SearchActivity.class);
                startActivity(intent);
                break;
        }
    }
}